package next.apps.biz;

import next.apps.bean.GoodsDetail;
import next.apps.bean.OrderInfo;
import next.apps.bean.StockInfo;
import next.apps.dao.StockDao;
import next.apps.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StockBizImpl implements StockBiz {
    @Autowired
    private StockDao stockDao;
    @Override
    public Map<String,Integer> mergeNumber(GoodsDetail detail) {
        Map<String,Integer> numberByGoodsIdMap = new HashMap<>();
        List<OrderInfo> orderInfoList = detail.getOrder();
        List<StockInfo> stockInfoList = detail.getStock();
        orderInfoList.forEach( item ->{
            if(numberByGoodsIdMap.containsKey(item.getGoodsId())){
                numberByGoodsIdMap.put(item.getGoodsId(),item.getNumber() + numberByGoodsIdMap.get(item.getGoodsId()));
            }else{
                numberByGoodsIdMap.put(item.getGoodsId(),item.getNumber());
            }
        });

        stockInfoList.forEach( item ->{
            if(numberByGoodsIdMap.containsKey(item.getGoodsId())){
                numberByGoodsIdMap.put(item.getGoodsId(),item.getNumber() + numberByGoodsIdMap.get(item.getGoodsId()));
            }else{
                numberByGoodsIdMap.put(item.getGoodsId(),item.getNumber());
            }
        });
        return numberByGoodsIdMap;
    }
    @Override
    public List<StockInfo> selectByGoodsId(List<String> goodsIds) {
        System.out.println("怎么找不到呢？？");
        return stockDao.selectList(goodsIds);
    }

    @Override
    public int updateByList(List<StockInfo> stockInfoList) {
        return stockDao.updateByList(stockInfoList);
    }
}
